import tcp from '../dxmodules/dxTcp.js'
import std from '../dxmodules/dxStd.js'
import logger from '../dxmodules/dxLogger.js'

// id
const id = "tcp"

// Connect to TCP service
tcp.create("192.168.60.166", "6000", 5000, 1, 60, id)

let timer = std.setInterval(() => {
    // Determine TCP connection status
    if (tcp.isConnect(id)) {
        std.clearInterval(timer)
        // After connecting, receive TCP data.
        std.setInterval(() => {
            let byteArr = tcp.receive(1, 100, id)
            if (byteArr && byteArr.length > 0) {
                logger.info(JSON.stringify(byteArr));
                // Send serial port data, return as is
                tcp.send("hello", id)
            }
        }, 10)
    }
}, 1000)